<template>
  <div class="login-page">
    <div class="header">
      <button class="back-btn" @click="goBack">返回</button>
      <button class="home-btn" @click="goHome">主页</button>
      <div class="title">登录</div>
    </div>

    <div class="login-container">
      <div class="input-group">
        <span class="phone-prefix">+86</span>
        <input
            type="tel"
            class="phone-input"
            placeholder="请输入手机号"
            v-model="phoneNumber"
        >
      </div>

      <div class="input-group">
        <span class="phone-prefix">验证码</span>
        <input
            class="pass-input"
            type="text"
            placeholder="请输入验证码"
            v-model="verifyCode"
        >
        <button
            class="verify-btn"
            @click="getVerifyCode"
            :disabled="isCountingDown"
        >
          {{ countDownText }}
        </button>
      </div>

      <button class="login-btn" @click="handleLogin">登录</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const phoneNumber = ref('')
const verifyCode = ref('')
const countDown = ref(0)
const isCountingDown = ref(false)
const countDownText = ref('获取验证码')

// 返回上一页
const goBack = () => {
  router.back()
}

// 跳转到首页
const goHome = () => {
  router.push('/')
}

// 获取验证码
const getVerifyCode = () => {
  if (!phoneNumber.value) {
    alert('请输入手机号')
    return
  }

  // 开始倒计时
  isCountingDown.value = true
  countDown.value = 60
  countDownText.value = `${countDown.value}秒后重试`

  const timer = setInterval(() => {
    if (countDown.value > 0) {
      countDown.value--
      countDownText.value = `${countDown.value}秒后重试`
    } else {
      clearInterval(timer)
      isCountingDown.value = false
      countDownText.value = '获取验证码'
    }
  }, 1000)

  // 这里添加获取验证码的接口调用
  console.log('获取验证码:', phoneNumber.value)
}

// 处理登录
const handleLogin = () => {
  if (!phoneNumber.value || !verifyCode.value) {
    alert('请填写完整信息')
    return
  }

  // 这里添加登录接口调用
  console.log('登录信息:', {
    phone: phoneNumber.value,
    code: verifyCode.value
  })
}
</script>

<style scoped>
.login-page {
  min-height: 100vh;
  background: #fff;
}

.header {
  padding: 15px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #eee;
}

.back-btn,
.home-btn {
  padding: 5px 15px;
  border: none;
  background: #f5f5f5;
  border-radius: 4px;
  margin-right: 10px;
}

.title {
  width: 100%;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
}

.login-container {
  padding: 15px;
  margin-top: 20px;
}

.input-group {
  margin-bottom: 15px;
  position: relative;
}

.phone-prefix {
  font-size: 13px;
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
}

input {
  width: 90%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
.phone-input {
  font-size: 10px;
  width: 78%;
  padding-left: 60px;

}
.pass-input {
  font-size: 10px;
  padding-left: 60px;
  width: 78%;
}

.verify-btn {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  padding: 5px 10px;
  background: #007AFF;
  color: white;
  border: none;
  border-radius: 4px;
}

.verify-btn:disabled {
  background: #ccc;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: #007AFF;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
}
</style>

